package cn.xizikelif.basic.service.impl;
import cn.xizikelif.basic.mapper.BaseMapper;
import cn.xizikelif.basic.query.BaseQuery;
import cn.xizikelif.basic.service.IBaseService;

import cn.xizikelif.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> mapper;

    @Override
    public T findById(Long id) {
        return mapper.findById(id);
    }

    @Override
    public List<T> findAll() {
        return mapper.findAll();
    }

    @Override
    @Transactional
    public void add(T t) {
        mapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        mapper.delete(id);
    }



    @Override
    public PageList<T> queryByPage(BaseQuery query) {
        // 1.获取分页总条数
        Integer totals = mapper.queryByCount(query);
        // 2.判断分页总条数是否有有数据，没有则返回 0 或者一个 []
        if(null == totals || 0 == totals){
            return new PageList<T>(0,new ArrayList<>());
        }
        // 3.获取分页数据
        List<T> rows = mapper.queryByPage(query);
        // 4.返回分页对象
        return new PageList<>(totals,rows);
    }

    @Override
    public Integer queryByCount(BaseQuery query) {
        return mapper.queryByCount(query);
    }
    
    @Override
    public void bachDelete(List<Long> ids) {
        mapper.bachDelete(ids);
    }
}
